package com.ricohgag.reaml;

import com.ricohgag.entity.AdminUser;
import com.ricohgag.service.AdminUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

//用户和权限验证的处理类
public class AuthReaml extends AuthorizingRealm {

	@Autowired
	private AdminUserService adminUserService;
	
	//角色和权限验证的方法
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		//添加角色信息
		SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();



		return simpleAuthorizationInfo;
	}

	//用户验证方法
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//用户名称
		String username= (String) token.getPrincipal();
		//获取密码
		String password=new String((char[])token.getCredentials());
		System.err.println("验证");
		AdminUser admin=adminUserService.checkAdminUser(username, password);
		if(admin==null) {
			throw new UnknownAccountException("用户名或密码不正确！");
		}

		if(admin.getStatus().intValue()==0) {
			throw new LockedAccountException("当前用户已经被禁止！");
		}
		//存储到session中
		SecurityUtils.getSubject().getSession().setAttribute("adminUserSession", admin);
		//表示验证成功了
		return new SimpleAuthenticationInfo(username,password,getName());
	}

}
